package com.heima.admin.controller.v1;

import com.heima.admin.service.ChannelService;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.validator.ValidatorAdd;
import com.heima.model.validator.ValidatorUpdate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/api/v1/channel")
@Api(value = "频道管理", tags = "频道管理", description = "频道管理API")
public class ChannelController {
    @Autowired
    private ChannelService channelService;

    @ApiOperation("查询全部频道")
    @GetMapping("/channels")
    public ResponseResult selectChannels() {
        List<AdChannel> list = channelService.list();
        return ResponseResult.okResult(list);
    }

    @PostMapping("/list")
    @ApiOperation("频道分页列表查询")
    public ResponseResult list(@RequestBody ChannelDTO dto) {

        return channelService.findListByQuery(dto);
    }


    @PostMapping("/save")
    @ApiOperation(value = "频道新增",notes = "保存时会校验，频道名称不能为空，字符长度不能超过10")
    public ResponseResult insert(@RequestBody AdChannel adChannel){
       return channelService.insert(adChannel);
    }


    @PostMapping("/update")
    @ApiOperation(value = "频道修改",notes = "新增时会校验，字符长度不能超过10")
    public ResponseResult update(@RequestBody AdChannel adChannel){
        return channelService.update(adChannel);
    }


    @GetMapping("/del/{id}")
    @ApiOperation(value = "根据id删除频道",notes = "如果频道有效，不能删除")
    public ResponseResult delete(@PathVariable("id") Integer id){
        return channelService.delete(id);
    }
    @ApiOperation("根据id查询频道")
    @GetMapping("/one/{id}")
    public ResponseResult findOne(@PathVariable Integer id) {
        return ResponseResult.okResult(channelService.getById(id));
    }
}
